package com.olinsports.system.olinsportssystem.util;

import com.olinsports.system.olinsportssystem.model.MultiCondition;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lcj on 2017/4/6.
 * 分页工具类
 */
@Component
public class PageUtil {
    //处理查找条件数据
    public List<MultiCondition> searchList(String page, String rows, boolean _search, HttpServletRequest request){
        // filters是获取查找的内容，格式如
        // filters = {"groupOp":"AND","rules":[{"field":"reporterKey","op":"eq","data":"32"}]} 这个是单个查询条件
        //groupOp AND是且的意思  OR是或的意思
        //  rules field 是查找的字段名  op 表达式的缩写  data是查找的条件
        //  filters: reporterKey = 32  也就是数据库表的where条件

        //  多个查询条件：filters = {"groupOp":"AND","rules":[{"field":"reporterKey","op":"eq","data":"3"},{"field":"reporterKey","op":"ne","data":"2"}]}

        int beg = (Integer.valueOf(page)-1)*Integer.valueOf(rows);
            //查询条件字符串
            String filters = request.getParameter("filters");

            //将字符串转为json格式
            JSONObject jsonObject = new JSONObject(filters);

            //获取key为groupOp的值 AND 和 OR
            String groupOp = jsonObject.getString("groupOp");

            //获取key 为rules的json值
            JSONArray rules = jsonObject.getJSONArray("rules");

            List<MultiCondition> multiList = new ArrayList<MultiCondition>();

            for(int i = 0;i < rules.length();i++){
                //将查询条件放到实体类multiCondition中
                MultiCondition multiCondition = new MultiCondition();
                JSONObject  rulesArray = rules.getJSONObject(i);
                //获取key 为field的值
                String field = rulesArray.getString("field");
                String op = rulesArray.getString("op");
                String data = rulesArray.getString("data");

                //表达式转换
                if (op.equals("eq")) {
                    op = "=";
                    data = "'"+data+"'";
                } else if (op.equals("ne")) {
                    op = "!=";
                    data = "'"+data+"'";
                } else if (op.equals("cn")) {
                    op = "like";
                    data = "'%" + data + "%'";
                } else if (op.equals("bw")) {
                    op = "like";
                    data = "'"+data + "%'";
                } else if (op.equals("ew")) {
                    op = "like";
                    data = "'%" + data +"'";
                }


                if(rules.length() == 1){//查询条件为一个
                    multiCondition.setGroupOp(null);
                } else {//查询条件多个
                    multiCondition.setGroupOp(groupOp);
                }
                System.out.println("field = "+field);
                multiCondition.setData(data);
                multiCondition.setField(field);

                multiCondition.setOp(op);
                multiList.add(multiCondition);
            }
            return multiList;
    }


    public Map paging(String rows, String page, int totalRows){
        int totalPages = 0;
        //page 当前页  rows每页显示的数量
        if ((totalRows % Integer.valueOf(rows)) == 0){
            totalPages = totalRows /  Integer.valueOf(rows);
        } else {
            totalPages = totalRows /  Integer.valueOf(rows) + 1;
        }
        Map map = new HashMap();
        map.put("total",totalPages);//设置总页数
        map.put("records",totalRows);//设置数据总数
        map.put("page",page);//设置页数

        return map;
    }
}
